package com.example.jl.bethinner.activity;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.jl.bethinner.R;
import com.example.jl.bethinner.adapter.SimpleListAdapter;


public class TestListActivity extends ActionBarActivity implements View.OnClickListener {
    private ListView listView;
    private TextView move_text_view;
    private TextView scale_text_view;
    private SimpleListAdapter simpleListAdapter;
    private boolean isAnimation;
    private int startY;
    private int text_startY;
    private TranslateAnimation anim;
    private boolean isOnScreen = true;
    private float width;
    private float height;
    private FloatingActionButton fab;
    private Animation operatingAnim;
    private Boolean isBig = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_list);

        //获取屏幕宽高
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        height = metric.heightPixels;
        width = metric.widthPixels;


        operatingAnim = AnimationUtils.loadAnimation(TestListActivity.this, R.anim.tip);
        LinearInterpolator lin = new LinearInterpolator();
        AccelerateInterpolator accelerateinterpolator = new AccelerateInterpolator();
        operatingAnim.setInterpolator(accelerateinterpolator);


        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(this);
        operatingAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (!isBig) {
//                    Animation scaleAnimation = new ScaleAnimation(1f, 2f, 1f, 2f,
//                            Animation.RELATIVE_TO_SELF, (fab.getX()+fab.getWidth()/2) / width,
//                            Animation.RELATIVE_TO_SELF, (fab.getY()+fab.getHeight()/2) / height);
//                    scaleAnimation.setDuration(200);
//                    scaleAnimation.setFillAfter(true);
//                    fab.startAnimation(scaleAnimation);
                    PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("scaleX", 1f, 1.3f);
                    PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleY", 1f, 1.3f);
                    ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(fab, pvhX, pvhY);
                    objectAnimator.start();
                    isBig = true;
                } else {
                    PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("scaleX", 1.3f, 1f);
                    PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleY", 1.3f, 1f);
                    ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(fab, pvhX, pvhY);
                    objectAnimator.start();
                    isBig = false;
                }

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        listView = (ListView) findViewById(R.id.listView);
        move_text_view = (TextView) findViewById(R.id.move_text_view);
        scale_text_view = (TextView) findViewById(R.id.scale_text_view);
        simpleListAdapter = new SimpleListAdapter(this, 20);
        listView.setAdapter(simpleListAdapter);


        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {


                Log.d("fvt", firstVisibleItem + "/" + visibleItemCount + "/" + totalItemCount);


            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(TestListActivity.this, "这个第" + position + "个", Toast.LENGTH_SHORT).show();
                int[] location = new int[2];
                view.getLocationOnScreen(location);
                float x = location[0] + view.getWidth() / 2;
                float y = location[1] + view.getHeight() / 2;
                Animation scaleAnimation = new ScaleAnimation(0f, 1f, 0f, 1f,
                        Animation.RELATIVE_TO_PARENT, x / width,
                        Animation.RELATIVE_TO_PARENT, y / height);
                scaleAnimation.setDuration(2000);
                scaleAnimation.setFillAfter(true);
                scale_text_view.setText("这个第" + position + "个");
                scale_text_view.startAnimation(scaleAnimation);
                listView.startAnimation(scaleAnimation);


            }
        });

        listView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                System.out.println("触摸..");
                if (event.getAction() == MotionEvent.ACTION_MOVE) {


                    if (text_startY - (int) event.getRawY() > 5) {
                        android.view.ViewGroup.LayoutParams params = scale_text_view
                                .getLayoutParams();
                        params.width = (5 + params.width);
                        params.height = (5 + params.height);
                        scale_text_view.setLayoutParams(params);
                        text_startY = (int) event.getRawY();
                    }

                    if (text_startY - (int) event.getRawY() < -5) {
                        android.view.ViewGroup.LayoutParams params = scale_text_view
                                .getLayoutParams();
                        params.width = (params.width - 5);
                        params.height = (params.height - 5);
                        scale_text_view.setLayoutParams(params);
                        text_startY = (int) event.getRawY();
                    }

                    if (startY - (int) event.getRawY() > 50) {
                        System.out.println("上移");  //接触到ListView移动时
                        startY = (int) event.getRawY();


                        //消失在屏幕上面的动画
                        ShowViewOnScreenAnim(move_text_view, false);

                    }

                    if (startY - (int) event.getRawY() < -50) {
                        System.out.println("下移");  //接触到ListView移动时
                        startY = (int) event.getRawY();
                        //出现在屏幕上面的动画

                        ShowViewOnScreenAnim(move_text_view, true);


                    }


//                        System.out.println("MOVE");  //接触到ListView移动时
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    System.out.println("up");   //离开ListView时
                } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    System.out.println("down");   //接触到ListView时
                    startY = (int) event.getRawY();
                    text_startY = (int) event.getRawY();
                    Log.d("getRawX", event.getRawX() + "");
                    Log.d("getRawY", event.getRawY() + "");
                }

                return false;
            }
        });


    }


    //view 一定是在布局的底部
    private void ShowViewOnScreenAnim(View view, final Boolean b) {
        if (b)
            anim = new TranslateAnimation(0, 0, view.getHeight(), 0);
        else
            anim = new TranslateAnimation(0, 0, 0, view.getHeight());
        anim.setFillAfter(true);
        anim.setDuration(200);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isAnimation = true;
                isOnScreen = b;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                fab.startAnimation(operatingAnim);
                isAnimation = false;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        if (!isAnimation && isOnScreen != b)
            view.startAnimation(anim);
        Log.d("view.getHeight", view.getHeight() + "");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_test_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Animation operatingAnim = AnimationUtils.loadAnimation(TestListActivity.this, R.anim.tip);
            LinearInterpolator lin = new LinearInterpolator();
            operatingAnim.setInterpolator(lin);
            fab.startAnimation(operatingAnim);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(final View v) {
        switch (v.getId()) {
            case R.id.fab:

                ShowViewOnScreenAnim(move_text_view, isBig);
                break;
        }


    }
}
